package com.ksy.common.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ksy.common.R;
import com.ksy.common.pick.OnWheelChangedListener;
import com.ksy.common.pick.WheelView;
import com.ksy.common.pick.adapters.ArrayWheelAdapter;
import com.ksy.common.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ksy.
 */

public class DateDialog extends CenterDialog implements OnWheelChangedListener {

    private ClickListener onClickListener;
    private List<Year> years = new ArrayList<>();
    private WheelView wheelSecond;
    private WheelView wheelFirst;
    private WheelView wheelThird;
    private String endDate;

    public DateDialog(Context context, String endDate) {
        super(context);
        this.endDate = endDate;
    }

    public DateDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_layout_dialog_date);
        View dialog_cancel = findViewById(R.id.dialog_cancel);
        View dialog_enter = findViewById(R.id.dialog_enter);
        wheelFirst = findViewById(R.id.wheelFirst);
        wheelSecond = findViewById(R.id.wheelSecond);
        wheelThird = findViewById(R.id.wheelThird);
        wheelFirst.addChangingListener(this);
        wheelSecond.addChangingListener(this);
        wheelThird.addChangingListener(this);
        dialog_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.cancel(DateDialog.this);
                }
            }
        });
        dialog_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Year year = null;
                Month month = null;
                Day day = null;
                int pCurrent = wheelFirst.getCurrentItem();
                int cCurrent = wheelSecond.getCurrentItem();
                int aCurrent = wheelThird.getCurrentItem();
                if (pCurrent >= 0 && pCurrent < years.size()) {
                    year = years.get(pCurrent);
                    List<Month> months = year.months;
                    if (months == null) {
                        months = new ArrayList<>();
                    }
                    if (cCurrent >= 0 && cCurrent < months.size()) {
                        month = months.get(cCurrent);
                        List<Day> areas = month.days;
                        if (areas == null) {
                            areas = new ArrayList<>();
                        }
                        if (aCurrent >= 0 && aCurrent < areas.size()) {
                            day = areas.get(aCurrent);
                        }
                    }
                }
                if (onClickListener != null) {
                    onClickListener.enter(DateDialog.this, year, month, day);
                }
            }
        });
        setUpData(endDate);
        updateMonth();
        updateDay();
    }

    private void updateDay() {
        List<Day> areas = null;
        int pCurrent = wheelFirst.getCurrentItem();
        if (pCurrent >= 0 && pCurrent < years.size()) {
            Year areaP = years.get(pCurrent);
            List<Month> months = areaP.months;
            if (months == null) {
                months = new ArrayList<>();
            }
            int cCurrent = wheelSecond.getCurrentItem();
            if (cCurrent >= 0 && cCurrent < months.size()) {
                Month area1 = months.get(cCurrent);
                areas = area1.days;
            }
        }
        if (areas == null) {
            areas = new ArrayList<>();
        }
        wheelThird.setViewAdapter(new ArrayWheelAdapter<>(getContext(), areas));
        wheelThird.setCurrentItem(0);
    }

    private void updateMonth() {
        List<Month> months = null;
        int pCurrent = wheelFirst.getCurrentItem();
        if (pCurrent >= 0 && pCurrent < years.size()) {
            Year areaP = years.get(pCurrent);
            months = areaP.months;
        }
        if (months == null) {
            months = new ArrayList<>();
        }
        wheelSecond.setViewAdapter(new ArrayWheelAdapter<>(getContext(), months));
        wheelSecond.setCurrentItem(0);
        updateDay();
    }

    private void setUpData(String endDate) {
        String limitDate = endDate;
        if (TextUtils.isEmpty(endDate)) {
            limitDate = DateUtil.getCurrentDate();
        }
        int yearEnd = DateUtil.getYear(limitDate);
        int monthEnd = DateUtil.getMonth(limitDate);
        int dayEnd = DateUtil.getDay(limitDate);

        if (yearEnd <= 1949) {
            yearEnd = DateUtil.getYear();
        }
        if (yearEnd <= 1949) {
            yearEnd = 2100;
        }

        for (int i = 1949; i <= yearEnd; i++) {
            Year year = new Year();
            year.year = i;
            year.months = new ArrayList<>();

            int monthTotal = 12;
            if (year.year == yearEnd) {
                monthTotal = monthEnd;
            }
            if (monthTotal <= 0) {
                monthTotal = 12;
            }
            for (int i1 = 1; i1 <= monthTotal; i1++) {
                Month month = new Month();
                month.month = i1;
                month.days = new ArrayList<>();

                int dayTotal = DateUtil.getDaySum(i, i1);
                if (month.month == monthEnd) {
                    dayTotal = dayEnd;
                }
                if (dayTotal <= 0) {
                    monthTotal = 31;
                }
                for (int i2 = 1; i2 <= dayTotal; i2++) {
                    Day day = new Day();
                    day.day = i2;
                    month.days.add(day);
                }
                year.months.add(month);
            }
            years.add(year);
        }
        wheelFirst.setViewAdapter(new ArrayWheelAdapter<>(getContext(), years));
        wheelFirst.setVisibleItems(7);
        wheelSecond.setVisibleItems(7);
        wheelThird.setVisibleItems(7);
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == wheelFirst) {
            updateMonth();
        } else if (wheel == wheelSecond) {
            updateDay();
        } else if (wheel == wheelThird) {

        }
    }


    public class Year {
        public int year;
        public List<Month> months;

        @Override
        public String toString() {
            return "" + year;
        }
    }

    public class Month {
        public int month;
        public List<Day> days;

        @Override
        public String toString() {
            if (month < 10) {
                return "0" + month;
            }
            return "" + month;
        }


    }

    public class Day {
        public int day;

        @Override
        public String toString() {
            if (day < 10) {
                return "0" + day;
            }
            return "" + day;
        }
    }

    public void setOnClickListener(ClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface ClickListener {
        void cancel(BaseDialog dialog);

        void enter(BaseDialog dialog, Year y, Month m, Day d);
    }
}
